package com.nus.servlets;

import com.sun.deploy.net.HttpRequest;
import oracle.net.ns.SessionAtts;
import org.apache.log4j.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 9/16/12
 * Time: 12:12 AM
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractServlet extends HttpServlet {
    protected Logger logger = Logger.getLogger(this.getClass());

    public Connection getLoginDatabaseConnection() {
        String dbConnectionName = getServletContext().getInitParameter("db_connection_name");
        return (Connection) getServletContext().getAttribute(dbConnectionName);
    }

    public String getConnectionURL() {
        return getServletContext().getInitParameter("db_url");
    }

    public Connection getUserDatabaseConnection(HttpServletRequest request) {
        DataSource dataSource = (DataSource) request.getSession().getAttribute("user_data_source");
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
        }
        return connection;
    }
}